$(function () {
    //初始化定位我的账户
    queryMyCouponData(1);
    lazyUserCenterImage();
    $("body").on("change","#type,#status",function () {
        queryMyCouponData(1);
    }).on('click','#coupon_list_context [data-coupon],.show-shop-box',function () {
        var _this = $(this);
        var couponId = _this.data('coupon');
        $.ajax({
            url : baseloaction + '/web/uc/op/showCouponShopData',
            type:'post',
            dataType:'json',
            data:{
                'couponId':couponId
            },
            success:function (result) {
                if(result.success==true){
                    var list = result.entity;
                    if(list==null || list.length ==0){
                        icModale(0,'','优惠券没有设置使用范围');
                    }else{
                        var context = '<div class="col-sm-12 mt10">';
                        $.each(list,function (index,shopData) {
                            context +='<div class="ml10">'+(index+1)+'、';
                            if(shopData.shopType=='COURSE'){
                                context +=shopData.shop.shopName +'【课程】'
                            }else if(shopData.shopType=='EXAM'){
                                context +=shopData.shop.shopName +'【试卷】'
                            }else if(shopData.shopType=='BOOK'){
                                context +=shopData.shop.shopName +'【图书】'
                            }else if(shopData.shopType=='MEMBER'){
                                context +=shopData.shop.shopName +'【会员】'
                            }else if(shopData.shopType=='QA'){
                                context +='限问答使用'
                            }else if(shopData.shopType=='ATTEND'){
                                context +='限旁听使用'
                            }
                            context +='</div>';
                        });
                        context +="</div>";
                        if(!_this.hasClass('show-shop-box')){
                            layer.closeAll();
                        }
                        layer.open({
                            type: 1,
                            area: ['680px'],
                            skin: 'layui-layer-demo',
                            title: '优惠券使用范围',
                            content:context ,
                            btn: '确定',
                            yes: function (index, layero) {
                                layer.close(index)
                            }
                        });
                    }
                }else{
                    icModale(0,'',result.message);
                }
            },
            error:function (error) {
                icModale(0,'','系统错误，显示优惠券使用范围失败');
            }
        });
    });

    /**
     * 加载用户图片
     */
    function lazyUserCenterImage() {
        //图片数据懒加载
        $('img.lazy').lazyload({
            placeholder: staticPath+'/static/front/web/v3/img/default/default-face.gif',  //占位图
            effect: 'fadeIn' //载入方式
        });
    }


    function queryMyCouponData(pageNo) {
        var params = $("#queryMyCoupon").serializeJSON();
        params = JSON.stringify(params);
        getPageAjaxData({
            url: baseloaction + '/web/uc/op/ajax/myAjaxCoupon',//请求URL
            contextId:'coupon_list_context',//响应内容页面设置内容ID
            params:params,//请求参数
            pageNo:pageNo,//页码
            pageCall:queryMyCouponData, //分页回调加载数据(点击上下页时调用,该方法也在加载页面完成时调用)
            selectAllId:'11', //全选框ID
            callback:function () {//数据加载完成回调

            }
        });
    }

    //获取优惠券
    myGetCouponFn()

    function myGetCouponFn() {
        $('body').on('click', '#getCouponBtn', function () {
            $.getReceiveCouponList();
        });
    }
});